<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: 16 条</span>
        </template>

        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click.native="showDialog=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px;">
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" sortable="" :index="indexMethod" type="index" />
          <el-table-column label="姓名" sortable="" prop="username" />
          <el-table-column label="手机号" sortable="" prop="mobile" />
          <el-table-column label="工号" sortable="" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatEmployment" sortable="" />
          <el-table-column label="部门" sortable="" prop="departmentName" />
          <el-table-column label="入职时间" sortable="" prop="timeOfEntry">
            <template #default="{row}">
              {{ row.timeOfEntry |formatTime }}

            </template>
          </el-table-column>
          <el-table-column label="操作" sortable="" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small">角色</el-button>
              <el-button type="text" size="small" @click="delEmployee(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            layout="total, sizes, prev, pager, next, jumper"

            :total="total"
            :current-page="page"
            :page-sizes="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"

            :page-size="pageSize"
            @size-change="handleSizeChange"

            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
      <add-employee
        :show-dialog.sync="showDialog"
      />
    </div>
  </div>
</template>

<script>
import { reqGetEmployeeList, reqDelEmployee } from '@/api/employees'
import AddEmployee from './components/add-employee.vue'

import obj from '@/constant/employees'

export default {
  name: 'Employees',
  components: {
    AddEmployee
  },
  data() {
    return {
      list: [],
      page: 1, // 当前页
      pageSize: 6, // 每页条数
      total: 0, // 总数
      loading: false,
      showDialog: false

    }
  },
  created() {
    this.getEmployeeList()
    // this.delEmployee()
  },
  methods: {
    handleCurrentChange(newPage) {
      this.page = newPage
      this.getEmployeeList()
    },
    // 每页条数变化
    handleSizeChange(value) {
      this.size = value
      this.page = 1
      this.getUserList()
    },
    async getEmployeeList() {
      this.loading = true
      const { data } = await reqGetEmployeeList(this.page, this.pageSize)
      const { total, rows } = data
      this.total = total
      this.rows = rows
      this.list = rows
      this.loading = false
    },

    indexMethod(index) {
      return index + 1 + (this.page - 1) * this.size
    },
    // 格式化聘用形式
    formatEmployment(row, column, cellValue, index) {
      const { hireType } = obj
      // 根据 cellValue(id) 去数组中，找相同id的项
      const result = hireType.find(item => item.id === +cellValue)
      // 常量抽离，将用于进行判断的标识，抽离到常量模块，将来直接导入使用，整个项目用一份常量
      return result ? result.value : '未知'
    },
    delEmployee(id) {
      // console.log(11)
      this.$confirm('确定要删除该员工吗？', '温馨提示').then(async() => {
        await reqDelEmployee(id)
        // console.log(res)
        this.$message.success('恭喜！删除成功！')
        if (this.list.length === 1 && this.page > 1) {
          this.page--
        }
        this.getEmployeeList()
      }).catch(() => {
        console.log('取消')
      })
    }

  }

}

</script>

<style>

</style>
